var mysql = require('mysql');
var pool = mysql.createPool({
	host: 'localhost',
	user: 'webadmin',
	password: '12344321',
	database: 'wephoto'
});
var queryString = [];
// create share_list table
queryString.push( 'CREATE TABLE IF NOT EXISTS share_list (' +
				  	'share_id int(11) NOT NULL AUTO_INCREMENT,' +
				  	'uri_map varchar(30),' +
				  	'share_content varchar(512),' +
	 			 	'timestamp varchar(45),' +
				  	'PRIMARY KEY (share_id)' +
				  ') ENGINE=InnoDB' );

// create photo_list table
queryString.push( 'CREATE TABLE IF NOT EXISTS photo_list (' +
				'photo_id int(11) NOT NULL AUTO_INCREMENT,' +
				'original_filename varchar(512),' +
				'filename varchar(512) NOT NULL,' +
				'event_id int(11) NOT NULL,' +
				'timestamp varchar(45),' +
				'is_hidden tinyint(4) DEFAULT 0,' +
				'is_portrait tinyint(4) DEFAULT 0,' +
				'PRIMARY KEY (photo_id)' +
				') ENGINE=InnoDB' );

// create print_list table
queryString.push( 'CREATE TABLE IF NOT EXISTS print_list (' +
				'print_id int(11) NOT NULL AUTO_INCREMENT,' +
				'photo_id int(11) NOT NULL,' +
				'event_id int(11) NOT NULL,' +
				'copies int(11) NOT NULL DEFAULT 1,' +
				'timestamp varchar(45),' +
				'is_fullfilled tinyint(4) DEFAULT 0,' +
				'PRIMARY KEY (print_id)' +
				') ENGINE=InnoDB' );

// create share_access table
queryString.push( 'CREATE TABLE IF NOT EXISTS share_access (' +
				'id int(11) NOT NULL AUTO_INCREMENT,' +
				'referer varchar(512),' +
				'event_id int(11) NOT NULL,' +
				'ip varchar(45),' +
				'uri_map varchar(30),' +
				'user_agent varchar(30) DEFAULT "other",' +
				'timestamp varchar(45),' +
				'PRIMARY KEY (id)' +
				') ENGINE=InnoDB' );

// create email_sent table
queryString.push( 'CREATE TABLE IF NOT EXISTS email_sent (' +
				'id int(11) NOT NULL AUTO_INCREMENT,' +
				'email varchar(512) NOT NULL,' +
				'name varchar(512),' +
				'uri_map varchar(30),' +
				'event_id int(11) NOT NULL,' +
				'is_success tinyint(4) DEFAULT 1,' + 
				'timestamp varchar(45),' +
				'PRIMARY KEY (id)' +
				') ENGINE=InnoDB' );

queryString.push( 'CREATE TABLE IF NOT EXISTS winners (' +
				'id int(11) NOT NULL AUTO_INCREMENT,' +
				'event_id int(11) NOT NULL,' +
				'random_code int(11) NOT NULL,' +
				'level tinyint(4),' + 
				'price varchar(512),' +
				'timestamp varchar(45),' +
				'PRIMARY KEY (id)' +
				') ENGINE=InnoDB' );

queryString.push( 'CREATE TABLE IF NOT EXISTS luckydraw (' +
				'id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,' +
				'event_id int(11) UNSIGNED NOT NULL,' +
				'random_code int(11) UNSIGNED NOT NULL,' +
				'state varchar(45) NOT NULL,' +
				'level tinyint(4),' + 
				'price varchar(512),' +
				'timestamp varchar(45),' +
				'PRIMARY KEY (id)' +
				') ENGINE=InnoDB' );

pool.getConnection(function(err, connection){
	queryString.forEach(function(qs){
		connection.query(qs, function(err, rows){
			console.log(arguments);
			if(err){
				console.log(err);
			}else{
				console.log("query ok!");
			}
		});
	});
});
